1 package org.apache.maven.surefire.junitcore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.Future;
29
30 import org.junit.runners.model.RunnerScheduler;
31
32
33
34
35 public class AsynchronousRunner
36 implements RunnerScheduler
37 {
38 private final List<Future<Object>> futures = Collections.synchronizedList( new ArrayList<Future<Object>>() );
39
40 private final ExecutorService fService;
41
42 public AsynchronousRunner( ExecutorService fService )
43 {
44 this.fService = fService;
45 }
46
47 public void schedule( final Runnable childStatement )
48 {
49 futures.add( fService.submit( Executors.callable( childStatement ) ) );
50 }
51
52
53 public void finished()
54 {
55 try
56 {
57 waitForCompletion();
58 }
59 catch ( ExecutionException e )
60 {
61 throw new RuntimeException( e );
62 }
63 }
64
65 public void waitForCompletion()
66 throws ExecutionException
67 {
68 for ( Future<Object> each : futures )
69 {
70 try
71 {
72 each.get();
73 }
74 catch ( InterruptedException e )
75 {
76 throw new RuntimeException( e );
77 }
78 }
79 }
80 }